package com.jml; 

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

/**
 * jwt设置过期时间
 */
public class JWTDemo03 {
    private static final String signKey = "jmlSign";

    public static void main(String[] args) {
        //当前时间
        long now = System.currentTimeMillis();
        //过期时间为1分钟
        long exp = now + 1000 * 5;
        JwtBuilder builder = Jwts.builder()
                .setIssuedAt(new Date())
                .claim("userId", "1234")
                .signWith(SignatureAlgorithm.HS256, signKey)
                //用于设置过期时间
                .setExpiration(new Date(exp));
        System.out.println(builder.compact());
        try {
            Thread.sleep(1000);
        } catch (Exception e) {

        }
        Claims body = Jwts.parser().setSigningKey(signKey).
                parseClaimsJws(builder.compact()).getBody();
        System.out.println(body);
    }
}
